﻿import Taro, { Component } from '@tarojs/taro'
import { Swiper, SwiperItem, Image, View, Text } from '@tarojs/components'
import '../../assets/common.scss'
import CommonJs from '../../utils/common'
import './region.scss'

export default class Index extends Component {
  constructor(props) {
    super(props);
    this.state = {
      regionlist:[],
      regionnum:'',
      active:0,
      province:'',
	  	byfrom:'',
	  	gradeSettingId:'',
	  	type:''
    }
  }
  componentWillMount () {
	var params=this.$router.params;
	var _this=this;
	if(params.byfrom=='join'){
		Taro.setNavigationBarTitle({title:"选择管辖区域"})
		this.setState({
			byfrom:1,
			gradeSettingId:params.gradeSettingId,
			type:params.type
		},function(){
			_this.getcancel()
		});
	}
	else{
		Taro.setNavigationBarTitle({title:"我的地址"})
		this.getcancel()
	}
    
  }
  getcancel(index){
    //获取省
    var _this=this;
	var list=this.state.regionlist 
	if(index||index==0){
		var id=list[index].id;
	}
	else{
		var id=144;
	}
    var url='/shop/papi/v1.9/region/findByParent/'+id;
	if(this.state.byfrom==1){ 
		url='/profit/papi/v1.9/parent-region-relate/regioByParent/'+id;
	}
	if(this.state.byfrom==1&&list.length>0&&list[index].level=='AREA'&&this.state.type!='sq'){
		if(list.length>0&&list[index].bindingStatus==2){
			Taro.showToast({
			  title: "此区域已经有绑定的易庄",
			  icon: "none",
			  duration: 1000,
			});
			return false;
		}
		 var url1='/order/papi/v1.9/regio-relate-order/price/gradeSetting/AREA'
		 var region=list[index]
		 Taro.setStorage({key:'region',data:region})
		 CommonJs.ajaxRequest(url1,'','GET',function(ret){
			if(ret.data){
				CommonJs.open('/pages/join/pay?byfrom=bdjoin&monery='+ret.data/100);
			}
			else if(ret.data==0){
				var url2='/order/bapi/v1.9/regio-relate-order/createAndPayment'
				var dates={
					payAmount:0,
					payTypeId:10
				}
				dates.regionId=list[index].id;
				dates.totalAmount=0
				dates.payAmount=0
				CommonJs.ajaxRequest(url2,dates,'POST',function(ret){
					Taro.showToast({
					  title: "绑定成功，跳转中。。。",
					  icon: "none",
					  duration: 1000,
					});
					setTimeout(function(){
						 CommonJs.open('/pages/my/my');
					},1000)
				},'wfw')
			}
		 },'wfw')  
		
	}
	else if(this.state.byfrom==1&&list.length>0&&list[index].level=='COUNTY'){
		if(list.length>0&&list[index].bindingStatus==2){
			Taro.showToast({
			  title: "此区域已经有绑定的易栈",
			  icon: "none",
			  duration: 1000,
			});
			return false;
		}
		
		 var url1='/order/papi/v1.9/regio-relate-order/price/gradeSetting/COUNTY'
		 var region=list[index]
		 Taro.setStorage({key:'region',data:region})
		 CommonJs.ajaxRequest(url1,'','GET',function(ret){
	
			if(ret.data){
				CommonJs.open('/pages/join/pay?byfrom=bdjoin&monery='+ret.data/100);
			}
			else if(ret.data==0){
				var url2='/order/bapi/v1.9/regio-relate-order/createAndPayment'
				var dates={
					payAmount:0,
					payTypeId:10
				}
				dates.regionId=list[index].id;
				dates.totalAmount=0
				dates.payAmount=0
				CommonJs.ajaxRequest(url2,dates,'POST',function(ret){
					Taro.showToast({
					  title: "绑定成功，跳转中。。。",
					  icon: "none",
					  duration: 1000,
					});
					setTimeout(function(){
						 CommonJs.open('/pages/my/my');
					},1000)
				},'wfw')
			}
			else{
				
			}
		 },'wfw')  
	}
	
	else{
		CommonJs.ajaxRequest(url,'','GET',function(res){
		  
		  if(res.data==null){
		  	Taro.showToast({
				  title: "该区域无数据了",
				  icon: "none",
				  duration: 1000,
				});
		  }
		  else if(res.data&&res.data.length==0){
				var region=list[index]
				Taro.setStorage({key:'region',data:region})
				_this.replaf()
		  }
		  else{
				_this.setState({
				  regionlist: res.data
				})
		  }
		},'wfw')
	}
  } 
  replaf(){
    Taro.navigateBack({
      delta: 1
    })
  }
  render () {
    const regionlist = this.state.regionlist
    return (
      <View className='pageblck'>
        <View className='province' style={this.state.active ==0?'display:block':'display:none'}>
        {regionlist.map((item, index) => (
          <View className='region' onClick={this.getcancel.bind(this,index)}>
          	<Text className={item.bindingStatus==2&&this.state.type!='sq'&&item.level=='AREA'||item.bindingStatus==2&&this.state.type=='sq'&&item.level=='COUNTY'?'region-text region-nochoose':'region-text'}>{item.name}</Text>
          	<Image mode='scaleToFill' className='region-img' src='https://minichat.g2ma.com/kedouimg/goselect.png'></Image>
          </View>
        ))}
        </View>
        
      </View>
    )
  }
}

 